package com.salestax.core.factory;

import java.util.Set;

import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import com.salestax.core.Product;
import com.salestax.core.specific.product.Book;
import com.salestax.core.specific.product.FoodProduct;
import com.salestax.core.specific.product.ImportedProduct;
import com.salestax.core.specific.product.MedicalProduct;
import com.salestax.core.specific.product.NormalProduct;

public class ProductFactory {
	
	private static Set<String> keywordsForBook = Sets.newHashSet("book","books");
	private static Set<String> keywordsForMedicalProducts = Sets.newHashSet("pill","pills","health");
	private static Set<String> keywordsForFoodProducts = Sets.newHashSet("chocolates","chocolate");
	private static Set<String> keywordsForImportedProducts = Sets.newHashSet("imported");

	public static Product createProduct(String description,	double basePrice,int quantity) {
		if(checkForKeyWord(description, keywordsForImportedProducts)) return new ImportedProduct(description,quantity,createProduct(reduceDescription(description, keywordsForImportedProducts), basePrice,1));
		if(checkForKeyWord(description, keywordsForFoodProducts)) return new FoodProduct(description,basePrice,quantity);
		if(checkForKeyWord(description, keywordsForBook)) return new Book(description,basePrice,quantity);
		if(checkForKeyWord(description, keywordsForMedicalProducts)) return new MedicalProduct(description,basePrice,quantity);
		return new NormalProduct(description,basePrice,quantity);
	}

	private static boolean checkForKeyWord(String description, Set<String> keywodList) {
		return !Sets.intersection(Sets.newHashSet(description.split(" ")), keywodList).isEmpty();
	}
	
	private static String reduceDescription(String description,Set<String> reducedKeywds){
		return Joiner.on(" ").join(Sets.difference(Sets.newHashSet(description.split(" ")), reducedKeywds));
	}

}
